home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerLisp 2.01 / Supplemental Documentation / Documentation / Chapter 18. Strings < prev    next >
Text File  |  1995-03-27  |  17KB  |  418 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 18. Strings
  5.  
  6. A string is a specialized vector (one-dimensional array) whose elements are
  7. characters.
  8.  
  9. [old_change_begin]
  10. Specifically, the type string is identical to the type (vector string-char),
  11. which in turn is the same as (array string-char (*)).
  12. [old_change_end]
  13.  
  14. [change_begin]
  15. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  16. string-char and to redefine the type string to be the union of one or more
  17. specialized vector types, the types of whose elements are subtypes of the type
  18. character.
  19. [change_end]
  20.  
  21. Any string-specific function defined in this chapter whose name begins with the
  22. prefix string will accept a symbol instead of a string as an argument provided
  23. that the operation never modifies that argument; the print name of the symbol
  24. is used.   In this respect the string-specific sequence operations are not
  25. simply specializations of generic versions; the generic sequence operations
  26. described in chapter 14 never accept symbols as sequences. This slight
  27. inelegance is permitted in Common Lisp in the name of pragmatic utility. One
  28. may get the effect of having a generic sequence function operate on either
  29. symbols or strings by applying the coercion function string to any argument
  30. whose data type is in doubt.
  31.  
  32. [change_begin]
  33. Note that this remark, predating the design of the Common Lisp Object System,
  34. uses the term ``generic'' in a generic sense and not necessarily in the
  35. technical sense used by CLOS (see chapter 2).
  36. [change_end]
  37.  
  38. Also, there is a slight non-parallelism in the names of string functions. Where
  39. the suffixes equalp and eql would be more appropriate, for historical
  40. compatibility the suffixes equal and = are used instead to indicate
  41. case-insensitive and case-sensitive character comparison, respectively.
  42.  
  43. Any Lisp object may be tested for being a string by the predicate stringp.
  44.  
  45. Note that strings, like all vectors, may have fill pointers (though such
  46. strings are not necessarily simple). String operations generally operate only
  47. on the active portion of the string (below the fill pointer). See fill-pointer
  48. and related functions.
  49.  
  50. -------------------------------------------------------------------------------
  51.  
  52.    *  String Access
  53.    *  String Comparison
  54.    *  String Construction and Manipulation
  55.  
  56. -------------------------------------------------------------------------------
  57.  
  58. 18.1. String Access
  59.  
  60. The following functions access a single character element of a string.
  61.  
  62. [Function]
  63. char string index
  64. schar simple-string index
  65.  
  66. The given index must be a non-negative integer less than the length of string,
  67. which must be a string. The character at position index of the string is
  68. returned as a character object. (This character will necessarily satisfy the
  69. predicate string-char-p.)
  70.  
  71. [change_begin]
  72. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate string-char-p.
  73. [change_end]
  74.  
  75. As with all sequences in Common Lisp, indexing is zero-origin. For example:
  76.  
  77. (char "Floob-Boober-Bab-Boober-Bubs" 0) => #\F
  78. (char "Floob-Boober-Bab-Boober-Bubs" 1) => #\l
  79.  
  80. See aref and elt. In effect,
  81.  
  82. (char s j) == (aref (the string s) j)
  83.  
  84. setf may be used with char to destructively replace a character within a
  85. string.
  86.  
  87. For char, the string may be any string; for schar, it must be a simple string.
  88. In some implementations of Common Lisp, the function schar may be faster than
  89. char when it is applicable.
  90.  
  91. -------------------------------------------------------------------------------
  92.  
  93. 18.2. String Comparison
  94.  
  95. The naming conventions for these functions and for their keyword arguments
  96. generally follow the conventions for the generic sequence functions (see
  97. chapter 14).
  98.  
  99. [change_begin]
  100. Note that this remark, predating the design of the Common Lisp Object System,
  101. uses the term ``generic'' in a generic sense and not necessarily in the
  102. technical sense used by CLOS (see chapter 2).
  103. [change_end]
  104.  
  105. [Function]
  106. string= string1 string2 &key :start1 :end1 :start2 :end2
  107.  
  108. string= compares two strings and is true if they are the same (corresponding
  109. characters are identical) but is false if they are not. The function equal
  110. calls string= if applied to two strings.
  111.  
  112. The keyword arguments :start1 and :start2 are the places in the strings to
  113. start the comparison. The arguments :end1 and :end2 are the places in the
  114. strings to stop comparing; comparison stops just before the position specified
  115. by a limit. The ``start'' arguments default to zero (beginning of string), and
  116. the ``end'' arguments (if either omitted or nil) default to the lengths of the
  117. strings (end of string), so that by default the entirety of each string is
  118. examined. These arguments are provided so that substrings can be compared
  119. efficiently.
  120.  
  121. string= is necessarily false if the (sub)strings being compared are of unequal
  122. length; that is, if
  123.  
  124. (not (= (- end1 start1) (- end2 start2)))
  125.  
  126. is true, then string= is false.
  127.  
  128. (string= "foo" "foo") is true
  129. (string= "foo" "Foo") is false
  130. (string= "foo" "bar") is false
  131. (string= "together" "frog" :start1 1 :end1 3 :start2 2)
  132.    is true
  133.  
  134. [change_begin]
  135. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  136. string).
  137. [change_end]
  138.  
  139. -------------------------------------------------------------------------------
  140. Compatibility note: string= is called strequal in Interlisp.
  141. -------------------------------------------------------------------------------
  142.  
  143. [Function]
  144. string-equal string1 string2 &key :start1 :end1 :start2 :end2
  145.  
  146. string-equal is just like string= except that differences in case are ignored;
  147. two characters are considered to be the same if char-equal is true of them. For
  148. example:
  149.  
  150. (string-equal "foo" "Foo") is true
  151.  
  152. [change_begin]
  153. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  154. string).
  155. [change_end]
  156.  
  157. [Function]
  158.  
  159. string< string1 string2 &key :start1 :end1 :start2 :end2
  160. string> string1 string2 &key :start1 :end1 :start2 :end2
  161. string<= string1 string2 &key :start1 :end1 :start2 :end2
  162. string>= string1 string2 &key :start1 :end1 :start2 :end2
  163. string/= string1 string2 &key :start1 :end1 :start2 :end2
  164.  
  165. These functions compare the two string arguments lexicographically, and the
  166. result is nil unless string1 is respectively less than, greater than, less than
  167. or equal to, greater than or equal to, or not equal to string2. If the
  168. condition is satisfied, however, then the result is the index within the
  169. strings of the first character position at which the strings fail to match; put
  170. another way, the result is the length of the longest common prefix of the
  171. strings.
  172.  
  173. A string a is less than a string b if in the first position in which they
  174. differ the character of a is less than the corresponding character of b
  175. according to the function char<, or if string a is a proper prefix of string b
  176. (of shorter length and matching in all the characters of a).
  177.  
  178. The keyword arguments :start1 and :start2 are the places in the strings to
  179. start the comparison. The keyword arguments :end1 and :end2 are the places in
  180. the strings to stop comparing; comparison stops just before the position
  181. specified by a limit. The ``start'' arguments default to zero (beginning of
  182. string), and the ``end'' arguments (if either omitted or nil) default to the
  183. lengths of the strings (end of string), so that by default the entirety of each
  184. string is examined. These arguments are provided so that substrings can be
  185. compared efficiently. The index returned in case of a mismatch is an index into
  186. string1.
  187.  
  188. [change_begin]
  189. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  190. string).
  191. [change_end]
  192.  
  193. [Function]
  194.  
  195. string-lessp string1 string2 &key :start1 :end1 :start2 :end2
  196. string-greaterp string1 string2 &key :start1 :end1 :start2 :end2
  197. string-not-greaterp string1 string2 &key :start1 :end1 :start2 :end2
  198. string-not-lessp string1 string2 &key :start1 :end1 :start2 :end2
  199. string-not-equal string1 string2 &key :start1 :end1 :start2 :end2
  200.  
  201. These are exactly like string<, string>, string<=, string>=, and string/=,
  202. respectively, except that distinctions between uppercase and lowercase letters
  203. are ignored. It is as if char-lessp were used instead of char< for comparing
  204. characters.
  205.  
  206. [change_begin]
  207. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  208. string).
  209. [change_end]
  210.  
  211. -------------------------------------------------------------------------------
  212.  
  213. 18.3. String Construction and Manipulation
  214.  
  215. Most of the interesting operations on strings may be performed with the generic
  216. sequence functions described in chapter 14. The following functions perform
  217. additional operations that are specific to strings.
  218.  
  219. [change_begin]
  220. Note that this remark, predating the design of the Common Lisp Object System,
  221. uses the term ``generic'' in a generic sense and not necessarily in the
  222. technical sense used by CLOS (see chapter 2).
  223. [change_end]
  224.  
  225. [old_change_begin]
  226.  
  227. [Function]
  228. make-string size &key :initial-element
  229.  
  230. This returns a string (in fact a simple string) of length size, each of whose
  231. characters has been initialized to the :initial-element argument. If an
  232. :initial-element argument is not specified, then the string will be initialized
  233. in an implementation-dependent way.
  234.  
  235. -------------------------------------------------------------------------------
  236. Implementation note: It may be convenient to initialize the string to null
  237. characters, or to spaces, or to garbage (``whatever was there'').
  238. -------------------------------------------------------------------------------
  239.  
  240. A string is really just a one-dimensional array of ``string characters'' (that
  241. is, those characters that are members of type string-char). More complex
  242. character arrays may be constructed using the function make-array.
  243. [old_change_end]
  244.  
  245. [change_begin]
  246. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  247. string-char and to add a keyword argument :element-type to make-string. The new
  248. function description is as follows.
  249.  
  250. [Function]
  251. make-string size &key :initial-element :element-type
  252.  
  253. This returns a simple string of length size, each of whose characters has been
  254. initialized to the :initial-element argument. If an :initial-element argument
  255. is not specified, then the string will be initialized in an
  256. implementation-dependent way.
  257.  
  258. The :element-type argument names the type of the elements of the string; a
  259. string is constructed of the most specialized type that can accommodate
  260. elements of the given type. If :element-type is omitted, the type character is
  261. the default.
  262.  
  263. X3J13 voted in January 1989 (ARGUMENTS-UNDERSPECIFIED)   to clarify that the
  264. size argument must be a non-negative integer less than the value of
  265. array-dimension-limit.
  266. [change_end]
  267.  
  268. [Function]
  269. string-trim character-bag string
  270. string-left-trim character-bag string
  271. string-right-trim character-bag string
  272.  
  273. string-trim returns a substring of string, with all characters in character-bag
  274. stripped off the beginning and end. The function string-left-trim is similar
  275. but strips characters off only the beginning; string-right-trim strips off only
  276. the end. The argument character-bag may be any sequence containing characters.
  277. For example:
  278.  
  279. (string-trim '(#\Space #\Tab #\Newline) " garbanzo beans
  280.         ") => "garbanzo beans"
  281. (string-trim " (*)" " ( *three (silly) words* ) ")
  282.    => "three (silly) words"
  283. (string-left-trim " (*)" " ( *three (silly) words* ) ")
  284.    => "three (silly) words* ) "
  285. (string-right-trim " (*)" " ( *three (silly) words* ) ")
  286.    => " ( *three (silly) words"
  287.  
  288. If no characters need to be trimmed from the string, then either the argument
  289. string itself or a copy of it may be returned, at the discretion of the
  290. implementation.
  291.  
  292. [change_begin]
  293. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  294. string).
  295. [change_end]
  296.  
  297. [Function]
  298. string-upcase string &key :start :end
  299. string-downcase string &key :start :end
  300. string-capitalize string &key :start :end
  301.  
  302. string-upcase returns a string just like string with all lowercase characters
  303. replaced by the corresponding uppercase characters. More precisely, each
  304. character of the result string is produced by applying the function char-upcase
  305. to the corresponding character of string.
  306.  
  307. string-downcase is similar, except that uppercase characters are converted to
  308. lowercase characters (using char-downcase).
  309.  
  310. The keyword arguments :start and :end delimit the portion of the string to be
  311. affected. The result is always of the same length as string, however.
  312.  
  313. The argument is not destroyed. However, if no characters in the argument
  314. require conversion, the result may be either the argument or a copy of it, at
  315. the implementation's discretion. For example:
  316.  
  317. (string-upcase "Dr. Livingstone, I presume?")
  318.    => "DR. LIVINGSTONE, I PRESUME?"
  319. (string-downcase "Dr. Livingstone, I presume?")
  320.    => "dr. livingstone, i presume?"
  321. (string-upcase "Dr. Livingstone, I presume?" :start 6 :end 10)
  322.    => "Dr. LiVINGstone, I presume?"
  323.  
  324. string-capitalize produces a copy of string such that, for every word in the
  325. copy, the first character of the word, if case-modifiable, is uppercase and any
  326. other case-modifiable characters in the word are lowercase. For the purposes of
  327. string-capitalize, a word is defined to be a consecutive subsequence consisting
  328. of alphanumeric characters or digits, delimited at each end either by a
  329. non-alphanumeric character or by an end of the string. For example:
  330.  
  331. (string-capitalize " hello ") => " Hello "
  332. (string-capitalize
  333.     Ø"occlUDeD cASEmenTs FOreSTAll iNADVertent DEFenestraTION")
  334. =>\>"Occluded Casements Forestall Inadvertent Defenestration"
  335. (string-capitalize 'kludgy-hash-search) => "Kludgy-Hash-Search"
  336. (string-capitalize "DON'T!") => "Don'T!"     ;not "Don't!"
  337. (string-capitalize "pipe 13a, foo16c") => "Pipe 13a, Foo16c"
  338.  
  339. [change_begin]
  340. X3J13 voted in June 1989 (STRING-COERCION)   to clarify string coercion (see
  341. string).
  342. [change_end]
  343.  
  344. -------------------------------------------------------------------------------
  345. Compatibility note: Some very approximate Interlisp equivalents to
  346. string-upcase, string-downcase, and string-capitalize are u-case, l-case with
  347. second argument nil, and l-case with second argument t.
  348. -------------------------------------------------------------------------------
  349.  
  350. [Function]
  351. nstring-upcase string &key :start :end
  352. nstring-downcase string &key :start :end
  353. nstring-capitalize string &key :start :end
  354.  
  355. These three functions are just like string-upcase, string-downcase, and
  356. string-capitalize but destructively modify the argument string by altering
  357. case-modifiable characters as necessary.
  358.  
  359. The keyword arguments :start and :end delimit the portion of the string to be
  360. affected. The argument string is returned as the result.
  361.  
  362. [Function]
  363. string x
  364.  
  365. Most of the string functions effectively apply string to such of their
  366. arguments as are supposed to be strings. If x is a string, it is returned. If x
  367. is a symbol, its print name is returned.
  368.  
  369. [old_change_begin]
  370. If x is a string character (a character of type string-char), then a string
  371. containing that one character is returned.
  372. [old_change_end]
  373.  
  374. [change_begin]
  375. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  376. string-char and to redefine the type string to be the union of one or more
  377. specialized vector types, the types of whose elements are subtypes of the type
  378. character. Presumably converting a character to a string always works according
  379. to this vote.
  380. [change_end]
  381.  
  382. In any other situation, an error is signaled.
  383.  
  384. To convert a sequence of characters to a string, use coerce. (Note that (coerce
  385. x 'string) will not succeed if x is a symbol. Conversely, string will not
  386. convert a list or other sequence to be a string.)
  387.  
  388. To get the string representation of a number or any other Lisp object, use
  389. prin1-to-string, princ-to-string, or format.
  390.  
  391. [change_begin]
  392. X3J13 voted in June 1989 (STRING-COERCION)   to specify that the following
  393. functions perform coercion on their string arguments identical to that
  394. performed by the function string.
  395.  
  396. string=       string-equal           string-trim
  397. string<       string-lessp           string-left-trim
  398. string>       string-greaterp        string-right-trim
  399. string<=      string-not-greaterp    string-upcase
  400. string>=      string-not-lessp       string-downcase
  401. string/=      string-not-equal       string-capitalize
  402.  
  403. Note that nstring-upcase, nstring-downcase, and nstring-capitalize are absent
  404. from this list; because they modify destructively, the argument must be a
  405. string.
  406.  
  407. As part of the same vote X3J13 specified that string may perform additional
  408. implementation-dependent coercions but the returned value must be of type
  409. string. Only when no coercion is defined, whether standard or
  410. implementation-dependent, is string required to signal an error, in which case
  411. the error condition must be of type type-error.
  412. [change_end]
  413.  
  414. -------------------------------------------------------------------------------
  415.  
  416.  
  417.  
  418.